package cn.project;

import cn.project.dao.UserDao;
import cn.project.dao.UserTempDao;
import cn.project.model.User;
import org.apache.commons.dbcp.BasicDataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class test {

    @Test
    public void demo1() throws SQLException {
        BasicDataSource bs = new BasicDataSource();
        bs.setDriverClassName("com.mysql.jdbc.Driver");
        bs.setUrl("jdbc:mysql://localhost:3306/news");
        bs.setUsername("root");
        bs.setPassword("admin");
        bs.setInitialSize(100);
        bs.setMaxActive(100);
        bs.setMaxIdle(10);
        bs.setMinIdle(2);
        bs.setMaxWait(18000);

        Connection connection = bs.getConnection();
        System.out.println(connection);
        System.out.println(bs.getInitialSize());
    }

    @Test
    public void demo2() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        List <User> list = ac.getBean("userDao", UserDao.class).getAllUser();
        for (User u : list) {
            System.out.println(u.getUserid() + "," + u.getUsername());
        }
    }
    /*
    * 利用jdbc模板进行插入 temp模板
    * */
@Test
    public void demo3(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserTempDao userTempDao=ac.getBean("userTemDao", UserTempDao.class);
        User user=new User();
        user.setUsername("张大胆");
        user.setPassword("123");
        userTempDao.save(user);
    }

    /*
    * 利用jdbc模板进行查询所有
    * */
    @Test
    public void demo4(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserTempDao userTempDao=ac.getBean("userTemDao", UserTempDao.class);
        List<User>list=userTempDao.getAllUser();
        for (User user :list) {
            System.out.println(user.getUserid()+","+user.getUsername());
        }
    }

    /*
    * 利用jdbc模板进行查询所有userTemDao
    * */
    @Test
    public void demo5(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserTempDao userTempDao=ac.getBean("userTemDao", UserTempDao.class);
        User use=new User();
        use.setUsername("张");
        List<User>list=userTempDao.getAllUser(use);
        for (User user :list) {
            System.out.println(user.getUserid()+","+user.getUsername());
        }
    }
    /*
   * 利用jdbc模板进行查询所有userTemDao
   * */
    @Test
    public void demo6(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserTempDao userTempDao=ac.getBean("userTemDao", UserTempDao.class);
        User user=userTempDao.findById(5);
        System.out.println(user.getUserid()+","+user.getUsername());

    }


}
